package com.talent.service.file.service;

import io.minio.*;
import io.minio.http.Method;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;

/**
 * @ClassName：MinioServiceImpl
 * @Author: hszhou
 * @Date: 2025/1/7 13:27
 * @Description: 必须描述类做什么事情, 实现什么功能
 */
@Service
public class MinioServiceImpl implements IMinioService{


    private static final Logger logger = LoggerFactory.getLogger(MinioServiceImpl.class);

    private final MinioClient minioClient;


    public MinioServiceImpl(MinioClient minioClient){
        this.minioClient =minioClient;
    }


    @Override
    public boolean isObjectExist(String bucketName, String objectName) {
        boolean exist = true;
        try {
            minioClient.statObject(StatObjectArgs.builder().bucket(bucketName).object(objectName).build());
        } catch (Exception e) {
            logger.error("[Minio工具类]>>>> 判断文件是否存在, 异常：", e);
            exist = false;
        }
        return exist;
    }

    @Override
    public InputStream getObject(String bucketName, String objectName, long offset, long length) {

        try{
            return minioClient.getObject(
                    GetObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .offset(offset)
                            .length(length)
                            .build());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public InputStream getObject(String bucketName, String objectName) {

        try{
            return minioClient.getObject(
                    GetObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .build());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    @Override
    public ObjectWriteResponse uploadFile(String bucketName, MultipartFile file) {
        try {
            InputStream inputStream = file.getInputStream();
            String fileName = file.getOriginalFilename();
            String newFileName = System.currentTimeMillis() + "." + StringUtils.substringAfterLast(fileName, ".");
            return minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(newFileName)
                            .contentType(file.getContentType())
                            .stream(inputStream, inputStream.available(), -1)
                            .build());
        }catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public ObjectWriteResponse uploadFile(String bucketName, String objectName, InputStream inputStream) {
        try {
            return minioClient.putObject(
                    PutObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .stream(inputStream, inputStream.available(), -1)
                            .build());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    public void removeFile(String bucketName, String objectName) {
        try {
            minioClient.removeObject(
                    RemoveObjectArgs.builder()
                            .bucket(bucketName)
                            .object(objectName)
                            .build());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    @Override
    public String getPresignedObjectUrl(String bucketName, String objectName) {

        GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder()
                .bucket(bucketName)
                .object(objectName)
                .method(Method.GET).build();
        try {
            return minioClient.getPresignedObjectUrl(args);
        }  catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getPresignedObjectUrl(String bucketName, String objectName, Integer expires) {
        GetPresignedObjectUrlArgs args = GetPresignedObjectUrlArgs.builder().expiry(expires).bucket(bucketName).object(objectName).build();
        try {
            return minioClient.getPresignedObjectUrl(args);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
